package com.huiminpay.paymentagent.service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.paymentagent.api.PayChannelAgentService;
import com.huiminpay.paymentagent.api.conf.AliConfigParam;
import com.huiminpay.paymentagent.api.dto.AlipayBean;
import com.huiminpay.paymentagent.api.dto.PaymentResponseDTO;
import org.apache.dubbo.config.annotation.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 有点儿意思
 * @Description:
 */
@Service
public class PayChannelAgentServiceImpl implements PayChannelAgentService {
    /**
     * 交易服务对接阿里支付接口
     * @param aliConfigParam 公共请求参数
     * @param alipayBean     业务请求参数
     * @return 支付结果 String类型for表单
     */
    @Override
    public PaymentResponseDTO<String> createPayOrderByAliWap(AliConfigParam aliConfigParam, AlipayBean alipayBean) {
        //获取公共请求参数
        //请求地址
        String serverUrl = aliConfigParam.getUrl();
        //支付宝应用id
        String appId = aliConfigParam.getAppId();
         //应用私钥
        String privateKey = aliConfigParam.getRsaPrivateKey();
         // 格式化方式
        String format = aliConfigParam.getFormat();
        // 设置字符集
        String charset = aliConfigParam.getCharest();

        //支付宝公钥
        String alipayPublicKey = aliConfigParam.getAlipayPublicKey();
        //签名类型
        String signType = aliConfigParam.getSigntype();
        // AlipayClient对象
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey,
                format, charset, alipayPublicKey, signType);
        // 手机网站支付的请求对象
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setReturnUrl(aliConfigParam.getReturnUrl());
        request.setNotifyUrl(aliConfigParam.getNotifyUrl());
        //获取业务请求参数
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setBody(alipayBean.getBody());
        model.setOutTradeNo(alipayBean.getOutTradeNo());
        model.setTotalAmount(alipayBean.getTotalAmount());
        model.setSubject(alipayBean.getSubject());
        //固定的产品码
        model.setProductCode(alipayBean.getProductCode());
        //交易过期时间
        model.setTimeoutExpress(alipayBean.getExpireTime());
        request.setBizModel(model);
        //请求阿里接口
        try {
            AlipayTradeWapPayResponse payResponse = alipayClient.pageExecute(request);
            String form = payResponse.getBody();

            //装入对象中返回
            PaymentResponseDTO<String> responseDTO = new PaymentResponseDTO<>();
            responseDTO.setContent(form);
            return responseDTO;
        } catch (AlipayApiException e) {
            throw new BusinessException(CommonErrorCode.E_400002);
        }
    }
}
